﻿using System;
using System.Collections.Generic;
using ZyGames.DZPocker.Model;
using ZyGames.Framework.Cache.Generic;
using ZyGames.Framework.Game.Cache;
using ZyGames.Framework.Game.Context;
using ZyGames.Framework.Game.Contract;
using ZyGames.Framework.Game.Contract.Action;
using ZyGames.Framework.Game.Lang;
using ZyGames.Framework.Game.Service;
using System.Linq;
using ZyGames.DZPocker.Bll.Logic;
using ZyGames.DZPocker.Script;
using System.Text.RegularExpressions;
using ZyGames.Framework.Net;
using ZyGames.Framework.Common.Serialization;
using ZyGames.DZPocker.Tools;

namespace ZyGames.DZPocker.Script.CsScript.Action
{
	/// <summary>
	/// 申请好友列表
	/// </summary>
	public class Action30073 : BaseAction
	{
		private int userId;
		private GameUser _currentGameUser;
		public Action30073(HttpGet actionGetter)
			: base((short)ActionType.DZApplyFriends, actionGetter)
		{
		}
		public override bool GetUrlElement()
		{
			if (httpGet.GetInt("UserId", ref userId))
			{
				return true;
			}
			return false;
		}

		public override bool TakeAction()
		{
			_currentGameUser = GameTable.Current.GetUser(userId);
			if (_currentGameUser == null)
			{
				Tool.WriteWarmingLine("消息30073玩家不存在");
				return false;
			}

			List<SimpleUserData> friendList = new List<SimpleUserData>();
			var friendsIdList = GameFriendShip.Current.GetFriendsId(userId);
			foreach(var uid in friendsIdList)
			{
				var user = GameTable.Current.GetUser(uid);
				if (user != null)
				{
					friendList.Add(new SimpleUserData() { UserId = uid, NickName = user.GetNickName(), HeadIcon = user.HeadIcon });
				}
			}

			//广播30074消息，给当前玩家下发好友列表
			var parameters = new Parameters();
			parameters["FriendsList"] = Convert.ToBase64String(ProtoBufUtils.Serialize(friendList));
			GameTable.Current.SyncNotifyAction((int)ActionType.DZShowFriends, _currentGameUser, parameters, t =>
			{
				Console.WriteLine("给某个人下发Action 30074 好友列表信息");
			});

			return true;
		}
	}
}
